{extend name="admin@public/layout/popup"}

{block name="body"}
<div id="login_form" style="display:none;">
<blockquote class="layui-elem-quote">注意：此处登录laytp官网账号。<a href="http://www.laytp.com/user/reg.html" target="_blank">现在注册</a>，就送99积分</blockquote>
<form class="layui-form layui-form-pane page-list-form" action="{$addons_api_url}/user/email_login" lay-filter="login">
    <div class="layui-form-item">
        <label class="layui-form-label" title="简称">邮箱 <text title="必填项" style="color:red;">*</text></label>
        <div class="layui-input-block">
            <input type="text" name="email" id="email" placeholder="请输入邮箱" class="layui-input" lay-verify="required" lay-verType="tips">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label" title="名称">密码 <text title="必填项" style="color:red;">*</text></label>
        <div class="layui-input-block">
            <input type="password" name="password" id="password" placeholder="请输入密码" class="layui-input" lay-verify="required" lay-verType="tips">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <a class="layui-btn layui-btn-sm layui-btn-normal" lay-submit lay-filter="login" id="login_submit_btn">提 交</a>
            <a href="http://www.laytp.com/user/reg.html" target="_blank" class="layui-btn layui-btn-sm layui-btn-primary">注册账号</a>
        </div>
    </div>
</form>
</div>
<div id="user" style="display:none;">
    <blockquote class="layui-elem-quote">
        您好,<span id="nickname"></span>,您当前的积分为:<span id="exp"></span><br />
        当前您已经登录，将同步保存你的购买记录<br />
    </blockquote>
    <blockquote class="layui-elem-quote">
        插件提供两种种付费方式:免费和积分购买<br />
        其中积分方式即使用官网积分进行插件购买<br />
        获取积分方式：<a href="http://www.laytp.com" target="_blank">进入官网</a>，每日签到和回答问题都可以获得积分<br />
    </blockquote>
</div>
{/block}

{block name="script"}
<script>
    layui.use(['layTp'],function() {
        const
            layTp = layui.layTp
            , $ = layui.jquery
        ;
        let addons_api_url = "{$addons_api_url}";
        let laytp_token = layTp.facade.getcookie('laytp_token');
        if(laytp_token){
            $.ajax({
                type: "POST",
                url: addons_api_url + "/user/info",
                data: {"token":laytp_token},
                dataType: 'json',
                success: function (res) {
                    if( res.code == 1 ){
                        $('#login_form').hide();
                        $('#user').show();
                        $('#nickname').html(res.data.nickname);
                        $('#exp').html(res.data.exp);
                    }else{
                        $('#login_form').show();
                        $('#user').hide();
                    }
                },
                error: function (xhr) {
                    if( xhr.status == '500' ){
                        layTp.facade.error('本地网络问题或者服务器错误');
                    }else if( xhr.status == '404' ){
                        layTp.facade.error('请求地址不存在');
                    }
                }
            });
        }else{
            $('#login_form').show();
            $('#user').hide();
        }

        layui.form.on('submit(login)', function(data){
            let form_action = $(data.form).attr('action');
            let form_method = $(data.form).attr('method');
            let type = (typeof form_method == "undefined") ? "POST" : form_method;
            let url = (typeof form_action == "undefined") ? window.location.href : form_action;
            $.ajax({
                type: type,
                url: url,
                data: data.field,
                dataType: 'json',
                success: function (res) {
                    if( res.code == 1 ){
                        //把token设置了
                        layTp.facade.setcookie('laytp_token',res.data.token,365);
                        layTp.facade.success(res.msg);
                        if(typeof res.data.reload === 'boolean' && res.data.reload){
                            setTimeout(function () {
                                location.reload();
                            }, 1000);
                        }
                        if(typeof parent.func_controller != "undefined"){
                            let index = parent.layer.getFrameIndex(window.name);
                            setTimeout(function () {
                                parent.layer.close(index);
                                parent.func_controller.table_render(parent.layui.form.val("laytp_search_form"),parent.$(".layui-laypage-em").next().html());
                            }, 1000);
                        }
                    }else{
                        layTp.facade.error(res.msg + ' ' + res.data);
                    }
                },
                error: function (xhr) {
                    if( xhr.status == '500' ){
                        layTp.facade.error('本地网络问题或者服务器错误');
                    }else if( xhr.status == '404' ){
                        layTp.facade.error('请求地址不存在');
                    }
                }
            });
        });

        $("#password").focus(function()
        {
            $(document).on("keydown",function(event)
            {
                if( event.keyCode == 13 )
                {
                    $('#login_submit_btn').click();
                }
            });
            $("#password").unbind();
        });
    });
</script>
{/block}